// ** Copyright (c) 2011* Freiburg, Germany * All rights reserved.
// ** File name：Calculator.cpp
// ** Current version：1.0 * Author：Yongkang Zhong * Finish time：22.05.2011
// ** Replace version：1.0 * Original author: Yongkang Zhong * Finish time：22.05.2011

#include "Calculator.h"
#include "EatSpaces.h"
#include "Expression.h"

using namespace std;

//const int MAX = 100;

// The program should allow as many successive calculations to be performed as required, and should
// terminate if an empty line is entered. It should also have helpful and friendly error messages.
int main()
{
    char buffer[MAX];
    
    cout << "Welcome to your friendly calculator..." << endl;
    cout << "Enter an algebraic expression(an empty line to quit):" << endl;
    
    for(;;)
    {
        double result = 0.0;
        int i = 0;
    
        cin.getline(buffer,MAX-1);
        
        if (!strcmp(buffer, ""))
            return 0;
    
        eatspaces(buffer);
        
        while(buffer[i])
        {
            if (!isdigit(buffer[i]) && buffer[i] != '.' && buffer[i] != '+' 
                && buffer[i] != '-' && buffer[i] != '*' && buffer[i] != '/')
            {
                cerr << "*** The format of expression is wrong." << " >> "; 
                cerr << i + 1 << ": \""<< buffer[i] << "\" >> Error(1) ***" << endl;
                exit(1);
            }
            
            i++;
        }
    
        result = expr(buffer);
    
        cout << "\t\t = " << result << endl;
    }
    
    return 0;
}
